-- summon_revive
-- created by zhuangsl
-- 召唤兽复活

return {
    apply = function(prop, baseValue)
        return baseValue;
    end,

    -- 死亡触发
    trigger = function(who, prop, arg)
        -- 如果不是召唤兽
        if who.type ~= OBJECT_TYPE_SUMMON then
            return;
        end

        -- 概率触发
        local rand = DungeonM.getRandSeed("summon_revive") % 1000;
        if (rand > prop[3]) then
            return;
        end

        -- 复活血量
        local maxHp = who:getMaxHp();
        local hp = maxHp;

        local classId = who.class_id;
        if SummonM.query(classId, "rule") == "azrael_cat" then
            local pId = PropM.getPropId("azrael");
            -- 男巫专属技能等级
            local p = PropM.combine(ME.user, pId, 1);
            local level = PropM.apply(p, 0);

            pId = PropM.getPropId("aid_attrib2");

            -- 记录阿兹猫的属性提升
            -- 先删掉旧的prop再重新记
            local hpProp = PropM.combine(ME.user, pId, "azrael_hp");
            local atkProp = PropM.combine(ME.user, pId, "azrael_attack");
            PropM.deleteProp(ME.user, "azrael");
            if level >= 1 then
                -- 生命值+20
                hpProp[3] = hpProp[3] + 20;
                PropM.record(ME.user, hpProp, "azrael");
            end
            if level >= 2 then
                -- 攻击+1
                atkProp[3] = atkProp[3] + 1;
                PropM.record(ME.user, atkProp, "azrael");
            end

            -- 阿兹猫复活，重新计算血量、攻击
            hp = 100;
            local attack = 5;
            local dungeonId = DungeonM.getDungeonId();
            local parentId = DungeonAreaM.getParentId(dungeonId);
            if DungeonAreaM.query(parentId, "type") == "main" then
                local layerFix = DungeonAreaM.query(dungeonId, "layer_fix");
                local fix = math.modf(layerFix / 4);
                hp = hp + 20 * fix;
            end

            p = PropM.combine(ME.user, "aid_attrib2", "azrael_hp");
            hp = PropM.apply(p, hp);

            -- 增加攻击
            who:addAttrib("attack", 1);
        end

        -- 设置血量
        who:setHp(hp);

        -- 如果是可以带下楼的，需要更新召唤兽信息
        local summonInfo = ME.user.dbase:query("summon");
        local pos = who:getPos();
        if summonInfo and summonInfo[pos] and
           summonInfo[pos]["class_id"] == classId and
           SummonM.query(classId, "carry_pass") == 1 then
            summonInfo[pos]["attack"] = who:query("attack");
            summonInfo[pos]["hp"] = hp;
        end

        return who:getHp() > 0;
    end,

    record = function(target, prop, path)
        return false;
    end
};
